package com.changgou.order.controller;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.config.TokenDecode;
import com.changgou.order.service.OrderService;
import com.changgou.order.pojo.Order;
import com.changgou.order.util.SMSUtil;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Order> orderList = orderService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",orderList) ;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Order findById(@PathVariable String id){
        Order order = orderService.findById(id);
        return order;
    }


    /***
     * 新增订单数据
     * @param order
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Order order){
        //1. 获取当前登录用户的用户名
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");

        //2. 调用service完整保存订单功能
        order.setUsername(username);
        orderService.add(order);
        return new Result(true,StatusCode.OK,"添加成功");
    }


    /***
     * 修改数据
     * @param order
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Order order,@PathVariable String id){
        order.setId(id);
        orderService.update(order);
        return new Result(true,StatusCode.OK,"修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        orderService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search" )
    public Result findList(@RequestParam Map searchMap){
        List<Order> list = orderService.findList(searchMap);
        return new Result(true,StatusCode.OK,"查询成功",list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestParam Map searchMap, @PathVariable  int page, @PathVariable  int size){
        Page<Order> pageList = orderService.findPage(searchMap, page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 管理员批量发货接口
     * @param orders
     * @return
     */
    @PostMapping("/batchSend")
    public Result batchSend(@RequestBody List<Order> orders) {
        orderService.batchSend(orders);
        return new Result(true, StatusCode.OK, "发货成功");
    }

    /**
     * 收货接口
     * @param orderId   收货的订单id
     * @param operater  收货人
     * @return
     */
    @GetMapping("/take/{orderId}/{operater}")
    public Result take(@PathVariable(value = "orderId") String orderId,
                       @PathVariable(value = "operater")String operater) {
        orderService.take(orderId, operater);
        return new Result(true, StatusCode.OK, "收货成功");
    }

    /**
     * 根据用户名查询对应订单信息
     * @return
     */
    @RequestMapping("/list")
    public List<Order> list(){
        //获取用户名
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");
        //调用service查询订单信息
        List<Order> orderList = orderService.list(username);

        return orderList;
    }

    /**
     * 获取所有未付款订单
     * @return
     */
    @RequestMapping("/waitPay")
    public List<Order> waitPay(){
        //获取用户名
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");
        //调用service获取订单对象集合
        List<Order> orderList = orderService.waitPay(username);

        return orderList;

    }

    /**
     * 待发货订单
     */
    @RequestMapping("/orderSend")
    public List<Order> orderSend(){
        //获取用户名
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");
        //调用service获取待发货订单集合
        List<Order> orderList = orderService.findOrderSend(username);

        return orderList;
    }

    /**
     * 获取所有待收货订单
     * @return
     */
    @RequestMapping("/orderReceive")
    public List<Order> orderReceive(){
        //获取用户名
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");
        //调用service获取待收货订单集合
        List<Order> orderList = orderService.orderReceive(username);
        return orderList;
    }

    /**
     * 确认收货
     * @param orderId
     * @return
     */
    @RequestMapping("/orderConfirm")
    public Result orderConfirm(@RequestParam("orderId")String orderId){
        //获取用户名
        Map<String, String> userInfo = tokenDecode.getUserInfo();
        String username = userInfo.get("username");

        //调用service根据订单id修改订单信息,确认收货
        orderService.orderConfirm(orderId,username);

        return new Result(true,StatusCode.OK,"确认收货成功");
    }

    /**
     * 提醒发货
     * @param orderId
     * @return
     */
    @RequestMapping("/remindSend")
    public Result remindSend(@RequestParam("orderId")String orderId){

        //调用service
        orderService.remindSend(orderId);

        return new Result(true,StatusCode.OK,"提醒发货成功");
    }

}
